//models define
package models

import (
	"git.oschina.net/YPTArch/usercenter/sys/db"
	"fmt"
	"database/sql"
	"errors"
)

var UserService *User = new(User)

//user struct define
type User struct {
	UserId            string //用户id
	UserName          string //用户名		50
	UserPwd           string //用户登录密码	36
	UserPhone         string //用户手机号		12
	UserEmail         string //用户电子邮件	50
	UserForeUpdatePwd int    //强制用户修改密码
}

type UserView struct {
	UserId            string //用户id
	UserName          string //用户名 	50
	UserPhone         string //用户手机号	12
	UserEmail         string //用户电子邮件	50
	UserForeUpdatePwd int    //强制用户修改密码
}
type LoginUser struct {
	LoginName string //登录名
	LoginPwd  string //登录密码
}

//create user
func (*User) Create(u *User) (bool, error) {
	tran, err := db.MySqlDB.DB.Begin()
	if err != nil {
		fmt.Println(err.Error())
		return false, errors.New("数据库开启事务异常")
	}
	//用户
	_, err = tran.Exec("INSERT INTO `User`(`UserId`,`UserName`,`UserPhone`,`UserEmail`,`UserPwd`)VALUES(?,?,?,?,?);",
		u.UserId, u.UserName, u.UserPhone, u.UserEmail, u.UserPwd)
	if err != nil {
		fmt.Println(err.Error())
		err = tran.Rollback()
		if err != nil {
			fmt.Println(err.Error())
			return false, errors.New("数据库事务回滚异常")
		}
		return false, errors.New("数据库事务执行异常")
	}
	err = tran.Commit()
	if err != nil {
		fmt.Println(err.Error())
		return false, errors.New("数据库事务执行异常")
	}
	return true, nil
}

func (*User) IsExistName(name string) (bool, error) {
	count, err := db.MySqlDB.QueryCount("select count(*) from User where UserName=?", name)
	return count > 0, err
}

func (*User) UpdatePwd(userId, pwd string) (bool, error) {
	err := db.MySqlDB.Exec("update User set UserPwd=? where UserId=?", pwd, userId)
	if err != nil {
		return false, err
	}
	return true, nil
}

func (*User) UpdateName(userId, name string) (bool, error) {
	err := db.MySqlDB.Exec("update User set UserName=? where UserId=?", name, userId)
	if err != nil {
		return false, err
	}
	return true, nil
}
func (*User) UpdateEmail(userId, email string) (bool, error) {
	err := db.MySqlDB.Exec("update User set UserEmail=? where UserId=?", email, userId)
	if err != nil {
		return false, err
	}
	return true, nil
}
func (*User) UpdatePhone(userId, phone string) (bool, error) {
	err := db.MySqlDB.Exec("update User set UserPhone=? where UserId=?", phone, userId)
	if err != nil {
		return false, errors.New("手机号修改失败")
	}
	return true, nil
}
func (*User) SetForeUpdatePwd(userId string,isFore int) (error) {
	err := db.MySqlDB.Exec("update User set UserForeUpdatePwd=? where UserId=?",isFore, userId)
	if err != nil {
		return errors.New("设置用户强制修改密码失败")
	}
	return nil
}

//
func (*User) IsExistEmail(email string) (bool, error) {
	count, err := db.MySqlDB.QueryCount("select count(*) from User where UserEmail=?", email)
	return count > 0, err
}

//
func (*User) IsExistPhone(phone string) (bool, error) {
	count, err := db.MySqlDB.QueryCount("select count(*) from User where UserPhone=?", phone)
	return count > 0, err
}

// query user by login
func (*User) GetUserByLogin(loginName string) (*User, error) {
	u := new(User)
	rowHandle := func(isHasRows bool, row *sql.Rows) error {
		if !isHasRows {
			u = nil
			return nil
		}
		err := row.Scan(&u.UserId, &u.UserName, &u.UserPhone, &u.UserEmail, &u.UserPwd,&u.UserForeUpdatePwd)
		return err
	}
	err := db.MySqlDB.QueryRow("select UserId,UserName,UserPhone,UserEmail,UserPwd,UserForeUpdatePwd from User where (UserPhone=? or UserEmail=? or UserName=? ) ", rowHandle, loginName, loginName, loginName)
	if err != nil {
		fmt.Println(err)
		return nil, err
	}
	return u, nil
}

// query user by id
func (*User) GetUserById(id string) (*User, error) {
	u := new(User)
	rowHandle := func(isHasRows bool, row *sql.Rows) error {
		if !isHasRows {
			u = nil
			return nil
		}
		err := row.Scan(&u.UserId, &u.UserName, &u.UserPhone, &u.UserEmail, &u.UserPwd,&u.UserForeUpdatePwd)
		return err
	}
	err := db.MySqlDB.QueryRow("select UserId,UserName,UserPhone,UserEmail,UserPwd,UserForeUpdatePwd from User where UserId=?", rowHandle, id)
	if err != nil {
		fmt.Println(err.Error())
		return nil, err
	}
	return u, nil
}
